import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Hi from '@/components/Hi'
import Hi1 from '@/components/Hi1'
import Hi2 from '@/components/Hi2'
import Params from '@/components/Params'
import Error from '@/components/Error'

Vue.use(Router)

export default new Router({
  // 有两种,一种是 hash  默认的,url中带有#,一种是history,不带#
  mode:'history',
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      components: {
        default:HelloWorld,
        view1:Hi1,
        view2:Hi2
      }
    },
    {
      path: '/params/:name/:age(\\d+)',
      name: 'Params',
      component: Params,
      // to:路由将要跳转的路径信息，信息是包含在对像里边的。
      // from:路径跳转前的路径信息，也是一个对象的形式。
      // next:路由的控制参数，常用的有next(true)和next(false)
      beforeEnter:(to,from,next)=>{
        console.log(to);
        console.log(from);
        //next(false);  不跳转
        next(true);   //跳转
        //next({path:'/'});  //重新指定跳转路径
      }
    },
    //重定向到首页 
    {
      path: '/gohome',
      redirect:'/'
    },
    // 带参数重定向
    {
      path: '/goparams/:name/:age(\\d+)',
      redirect:'/params/:name/:age(\\d+)'
    },
    {
      path:'/hi',
      name:'Hi',
      alias:'/goHi',
      component:Hi,
      children:[
        {
          path:'1',
          name:'Hi1',
          component:Hi1
        },{
          path:'2',
          name:'Hi2',
          component:Hi2
        }
      ]
    },
    {
      path:'*',
      name:'Error',
      component:Error
    }
  ]
})
